package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.ALiYunSms;
import com.ruoyi.common.utils.BaiDuSms;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Member;
import com.ruoyi.system.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Customer;
import com.ruoyi.system.domain.RelationshipRecord;
import com.ruoyi.system.service.ICustomerService;
import com.ruoyi.system.service.IRelationshipRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 联系记录Controller
 * 
 * @author ruoyi
 * @date 2019-10-14
 */
@Controller
@RequestMapping("/system/record")
public class RelationshipRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private IRelationshipRecordService relationshipRecordService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICustomerService customerService;
    
    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询联系记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RelationshipRecord relationshipRecord)
    {
        startPage();
        List<RelationshipRecord> list = relationshipRecordService.selectRelationshipRecordList(relationshipRecord);
        return getDataTable(list);
    }

    /**
     * 导出联系记录列表
     */
    @RequiresPermissions("system:record:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RelationshipRecord relationshipRecord)
    {
        List<RelationshipRecord> list = relationshipRecordService.selectRelationshipRecordList(relationshipRecord);
        ExcelUtil<RelationshipRecord> util = new ExcelUtil<RelationshipRecord>(RelationshipRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增联系记录
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
        Customer customer = customerService.selectCustomerById(id);
        mmap.put("customer", customer);
        return prefix + "/add";
    }

    /**
     * 新增保存联系记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "联系记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RelationshipRecord relationshipRecord)
    {
        Customer customer = customerService.selectCustomerById(relationshipRecord.getCustomerId());
        customer.setRelationTime(DateUtils.getNowDate());
        customerService.updateCustomer(customer);
        if(customer!=null){
            Member selectMemberById = memberService.selectMemberById(customer.getMemberId());
            if(selectMemberById!=null){
                try {
                    ALiYunSms.sendSms(selectMemberById.getTelephone(), selectMemberById.getNickName());
                    //BaiDuSms.sendSms(selectMemberById.getTelephone(), selectMemberById.getNickName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return toAjax(relationshipRecordService.insertRelationshipRecord(relationshipRecord));
    }

    /**
     * 修改联系记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RelationshipRecord relationshipRecord = relationshipRecordService.selectRelationshipRecordById(id);
        mmap.put("relationshipRecord", relationshipRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存联系记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "联系记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RelationshipRecord relationshipRecord)
    {
        return toAjax(relationshipRecordService.updateRelationshipRecord(relationshipRecord));
    }

    /**
     * 删除联系记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "联系记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(relationshipRecordService.deleteRelationshipRecordByIds(ids));
    }
}
